<template>
  <div>
    <el-card>
      <el-row type="flex" justify="space-between" align="middle" class="titleelrow">

        <el-button type="primary" @click="addUser">添加客户</el-button>
        <el-row>
          <el-select v-model="pageConfig.searchParams" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-input v-model="pageConfig.name_contains" placeholder="请输入内容" style="width:500px" />
          <el-button type="primary" plain @click="btnsearch">
            筛选
          </el-button>
        </el-row>

      </el-row>
      <hr>
      <template>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column label="头像" sortable="" prop="image" width="180px">
            <template #default="obj">
              <img :src="obj.row.image" alt="" style="width:100%;height:100%">
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称"
            width="150"
          />
          <el-table-column
            prop="email"
            label="邮箱"
            width="150"
          />
          <el-table-column
            prop="phone"
            label="电话"
            width="175"
          />
          <el-table-column
            prop="agent.name"
            label="经纪"
            width="175"
          />
          <el-table-column
            prop="wechat"
            label="微信"
            width="175"
          />
          <el-table-column
            prop="desc"
            label="介绍"
            width="175"
          />
          <el-table-column
            prop=""

            label="操作"
            width="175"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="delUser(scope.row.id)">删除</el-button>
              <el-button type="text" size="small" @click="edito(scope.row.id)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="prev, pager, next, total"
          :page-size.sync="pageConfig._limit"
          :total="total"
          :current-page="currentPage"
          @current-change="currentChange"
          @size-change="sizeChange"
        />
      </el-row>

    </el-card>
    <!-- 弹窗 -->
    <addUser ref="addForm" :show-dialog.sync="showDialog" :user-list="userList" @addDepartments="addDepartments" />

  </div>
</template>

<script>
import addUser from '@/views/client/addUser'
import { getClients, delClients, countClients, getAgents, getendio } from '@/api/client'
export default {
  components: { addUser },

  data() {
    return {
      pageConfig: {
        _start: 0,
        _limit: 2,
        searchParams: '', // 经纪人id
        name_contains: '' // 经纪人名字
      },
      currentPage: 0,
      userList: {},
      total: 0,
      showDialog: false,
      tableData: [],

      options: []
    }
  },
  async created() {
    const res = await getAgents()
    console.log(118, res)
    this.options = res.data
    // unshift在对象最前面加进去
    this.options.unshift({ name: '查看全部', id: '' })
    this.getClients()
  },
  methods: {
    // 编辑
    async edito(id) {
      this.showDialog = true
      const List = await getendio(id)
      this.userList = List.data
    },
    // 查询
    btnsearch() {
      // 先初始化在查询
      this.currentPage = 1
      this.pageConfig._start = 0
      this.pageConfig._limit = 2
      this.getClients()
    },
    sizeChange(newPageSize) {
      this.pageConfig._limit = newPageSize
      this.getClients()
    },
    currentChange(newPage) {
      // console.log((newPage - 1) * this.pageConfig._limit)
      this.currentPage = newPage
      this.pageConfig._start = (newPage - 1) * this.pageConfig._limit

      this.getClients()
    },
    addDepartments() {
      this.getClients()
    },
    // 获取数据
    async getClients() {
      const res = await getClients(this.pageConfig)
      console.log(144, res.data)
      this.tableData = res.data
      // const res2 = await getAgents()
      // console.log(res2)
      // const name = res2.data.filter(item => {
      //   return item.id === this.pageConfig.searchParams
      // })
      // console.log(name)
      // 根据经纪人id可获取该经纪人对应的数据的数量

      const result = await countClients({ id: this.pageConfig.searchParams, name: this.pageConfig.name_contains })
      console.log(168, result)
      if (!this.pageConfig.searchParams && !this.pageConfig.name_contains) {
        this.total = result.data - 1
      } else {
        this.total = result.data
      }
      console.log(result)
      // this.total = result.data
    },
    async delUser(id) {
      // 清除表单校验
      // this.$refs.addForm.$refs.addForm.resetFields()
      await this.$confirm('确定删除吗')

      await delClients(id)

      // 页码搞定,别动他
      console.log(195, this.total)
       if (this.total - 1 === this.pageConfig._start) {
        this.pageConfig._start = this.pageConfig._start > 0 ? this.pageConfig._start - this.pageConfig._limit : 0
        // Math.ceil(this.pageConfig._start - this.pageConfig._limit)
      }
      this.currentPage = this.pageConfig._limit < 1 ? this.currentPage - 1 : this.currentPage
      this.$message.success('删除成功')
      // this.total = this.total - 1
      this.getClients()
    },
    addUser() {
      this.showDialog = true
    }
  }

}
</script>

<style>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  .titleelrow{
    padding: 0 20px;
    padding-bottom: 10px;
  }
  hr{
   background-color: #dcdfe6;
  }

</style>
